﻿using Inovout.Cloud.Applications.Security.Models;
using Inovout.Cloud.Applications.Security.Services;
using Inovout.Cloud.Console.Web.Models;
using Inovout.Web.Mvc;
using System.Web.Mvc;

namespace Inovout.Cloud.Console.Web.Controllers
{
    public class ServerApplicationCredentialController : CredentialController<ServerApplicationCredential>
    {
        [HttpPost]
        public string UpdateApiKey(string ServerApplicationCredentialId)
        {
            ServerApplicationCredential webApplicationCredential = base.RepositoryService.FindById(int.Parse(ServerApplicationCredentialId));
            IServerApplicationCredentialService serverApplicationCredentialServer = (ServerApplicationCredentialService)base.RepositoryService;
            serverApplicationCredentialServer.UpdateApiKey(webApplicationCredential);
            if (base.Request.IsAjaxRequest())
            {
                return webApplicationCredential.ApiKey.ToString();
            }
            else
            {
                return "";
            }
        }

        [HttpPost]
        public ActionResult Update(ServerApplicationCredential serverApplicationCredential, string projectId)
        {
            IServerApplicationCredentialService serverApplicationCredentialService = (ServerApplicationCredentialService)base.RepositoryService;
            serverApplicationCredentialService.Update(serverApplicationCredential);
            return RedirectToAction("My", "Credential", new { projectId = projectId });
        }

        public ActionResult Delete(int id, string projectId)
        {
            IServerApplicationCredentialService serverApplicationCredentialService = (ServerApplicationCredentialService)base.RepositoryService;
            serverApplicationCredentialService.Delete(id);
            return RedirectToAction("My", "Credential", new { projectId = projectId });
        }
    }
}